package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.prism.shader.Solid_TextureYV12_AlphaTest_Loader;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.form.AddClassroomForm;
import com.woniu.model.form.PageForm;
import com.woniu.model.model.YVenueClassroom;
import com.woniu.model.param.PageAllClassroomParam;
import com.woniu.model.param.PageParam;
import com.woniu.service.YVenueClassroomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author EdisonLiu
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yVenueClassroom")
@Api(tags = "场馆的教室接口")
public class YVenueClassroomController {
    @Resource
    private YVenueClassroomService venueClassroomService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 根据id查询教室信息
     * @param id 教室的id
     * @return
     */

    @GetMapping("/loadClassroomById/{id}")
    @ApiOperation(value = "根据id查询教室")
    public Result<?> loadClassroomById(@PathVariable Integer id)throws YogaException{

        YVenueClassroom venueClassroom = venueClassroomService.loadClassroomById(id);

        return Result.newInstance(YVenueClassroom.class).setData(venueClassroom);
    }

    /**
     * 根据场馆id查询教室list
     * @param venueId 场馆id
     * @return
     */
    @GetMapping("/loadClassroomByVenueId")
    @ApiOperation(value = "根据场馆id查询教室")
    public Result<?> loadClassroomByVenueId(Integer venueId){
        List<YVenueClassroom> venueClassroom = venueClassroomService.loadClassroomByVenueId(venueId);
        return Result.newListInstance(YVenueClassroom.class).setData(venueClassroom);
    }

    /**
     * 根据场馆id查询教室list
     * @param
     * @return
     */
    @GetMapping("/loadClassroomByTokenId")
    @ApiOperation(value = "根据场馆id查询教室")
    public Result<?> loadClassroomByTokenId(@RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        List<YVenueClassroom> venueClassroom = venueClassroomService.loadClassroomByVenueId(venueId);
        return Result.newListInstance(YVenueClassroom.class).setData(venueClassroom);
    }

    /**
     * 分页查询所有类型
     * @param form
     * @return
     * @throws Exception
     */
    @GetMapping("/pageAllClassroom")
    @ApiOperation("分页查询所有类型")
    public Result<Page<YVenueClassroom>> pageAllClassroom(PageForm form,@RequestHeader String token) throws YogaException {
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        PageAllClassroomParam param = BeanUtil.toBean(form, PageAllClassroomParam.class);
        param.setVenueId(venueId);

        Page<YVenueClassroom> page = venueClassroomService.pageAllClassroom(param);
        return Result.newPageInstance(YVenueClassroom.class).setMsg("查询成功").setData(page).setCode(200);
    }

    /**
     * 场馆新增教室
     * @param form
     * @return
     */
    @PostMapping("/addClassroom")
    @ApiOperation(value = "新增教室")
    public Result<?> addClassroom(@RequestBody AddClassroomForm form,@RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());
        form.setVenueId(venueId);

        Integer i =venueClassroomService.addClassroom(form);
        if (!i.equals(1)){
            return Result.getResult().setMsg("新增教室失败").setCode(500);
        }
        return Result.getResult().setMsg("新增教室成功");
    }

    /**
     * 根据id教室弃用
     * @param id 教室id（非场馆的教室id）
     * @return
     */
    @GetMapping("/dropClassroom")
    @ApiOperation(value = "教室弃用")
    public Result<?> dropClassroom(Integer id){
        Integer i =venueClassroomService.dropClassroom(id);
        if (!i.equals(1)){
            return Result.getResult().setMsg("弃用教室失败").setCode(500);
        }
        return Result.getResult().setMsg("弃用教室成功");
    }

    /**
     * 根据id教室启用
     * @param id 教室id（非场馆的教室id）
     * @return
     */
    @GetMapping("/enableClassroom")
    @ApiOperation(value = "教室弃用")
    public Result<?> enableClassroom(Integer id){
        Integer i =venueClassroomService.enableClassroom(id);
        if (!i.equals(1)){
            return Result.getResult().setMsg("启用教室失败").setCode(500);
        }
        return Result.getResult().setMsg("启用教室成功");
    }





}

